//var elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch'));

var elem = document.querySelector('.js-switch');
var elem_t = document.querySelector('.js-switch_t');
var elem_o = document.querySelector('.js-switch_o');
var switchery,switchery_t;
$(document).ready(function() {
	switchery = new Switchery(elem, { size: 'small' });
	switchery_t = new Switchery(elem_t, { size: 'small' });
	switchery_o = new Switchery(elem_o, { size: 'small' });
 });


var clickCheckbox = document.querySelector('.js-check-click');
var clickCheckbox_t = document.querySelector('.js-check-click_t');
var clickCheckbox_o = document.querySelector('.js-check-click_o');
clickCheckbox.onchange = function() {
	switchery.disable();
	var id = this.id;
	bootbox.confirm("确定修改当前支付设置吗?", function(result) {
		if(result){
       		SettingCheckboxClick(id)
       	}else{
       		switchery.enable();
       		location.reload();
       	}
        $(this).closeDialog(modal); 
    });
};
clickCheckbox_t.onchange = function() {
	switchery_t.disable();
	var id = this.id;
	bootbox.confirm("确定修改当前支付设置吗?", function(result) {
		if(result){
       		SettingCheckboxClick(id)
       	}else{
       		switchery_t.enable();
       		location.reload();
       	}
        $(this).closeDialog(modal); 
    });
};
clickCheckbox_o.onchange = function() {
	switchery_o.disable();
	var id = this.id;
	bootbox.confirm("确定修改当前回访设置吗?", function(result) {
		if(result){
       		SettingCheckboxClick(id)
       	}else{
       		switchery_o.enable();
       		location.reload();
       	}
        $(this).closeDialog(modal); 
    });
};
$("#employeeSearch").autocomplete({
    source: function(query,process){
    	$.ajax({
        	"contentType":"application/json; charset=UTF-8",
            "dataType" : 'json',
            "type" : "POST",
            "url" : ctx + "/orders/employee/"+query.term,
            "data" : {"matchCount":200},
            "success" : function(data){
            	var ebean = []
            	var length = data.length >= 10 ? 10 : data.length;
            	for (var int = 0; int < length; int++) {
            		var temp = [];
            		temp.label = data[int].employeeName + "	"+ data[int].employeeNo;
            		temp.value = data[int].employeeId;
            		ebean.push(temp);
        		}
                return process(ebean);
            }
        })
    },
    search:function(event, ui){
    	console.log("searched!");
//    	$("#employeeId").val("");
    },
    select: function(event, ui) {
        $("#employeeSearch").val(ui.item.label);
        $("#employeeId").val(ui.item.value);
        var uistr =ui.item;
        // 阻止事件的默认行为
        event.preventDefault(); 
    }
});
$("input#employeeSearch").focus(function(){
	$(this).autocomplete("search", "TOTAL");
	event.preventDefault(); 
});
$("input#employeeSearch").blur(function(){
	event.preventDefault(); 
});
//设置理财师
$('#queryBtn').click(function(){
	var modal = $('#distributionModal').modal('show');
	modal.on("hidden.bs.modal",function(e){
		$(this).removeData();
	});
	$("#distributionConfirm").unbind("click").click(function(){
		var json = $("form#setDefaultEmployeeForm").serializeJSON();
		$.ajax({
	    	"contentType":"application/json; charset=UTF-8",
	        "dataType" : 'json',
	        "type" : "POST",
	        "url" : ctx + "/setting/setDefaultEmployee" ,
	        "data" : json ,
	        "success" : function(msg){
	        	if (msg.code == 1) {
	        		bootbox.alert("设置成功！");
            		modal.modal('hide');
	        		location.reload();
	        	} else {
	        		bootbox.alert(msg.message);
	        	}
	        },
	        "error": function (msg) {
				bootbox.alert(msg.message);
				swal("操作错误", "", "error");
		   }
	    })
	});
});

$('#copyButton').each(function(){
	var textCopy = microWebsitePrefix + $('label[name="microCode"]').attr('id');
	var clip = new ZeroClipboard.Client(); // 新建一个对象
	clip.setHandCursor(true);
	clip.setText(textCopy); // 设置要复制的文本。
	clip.addEventListener("mouseUp", function(client) {
		bootbox.alert("复制链接成功！");
	});
	// 注册一个 button，参数为 id。点击这个 button 就会复制。
	//这个 button 不一定要求是一个 input 按钮，也可以是其他 DOM 元素。
	clip.glue(this.id); // 和上一句位置不可调换
});
//$('#copyButton').click();
$('#view').click(function(){
	window.open(microWebsitePrefix + $('label[name="microCode"]').attr('id'));
})
 

// alert($(this).text())
function SettingCheckboxClick(id){
 	 var flag = $('#'+id+'').is(':checked');
 	 var key_value = 0;
 	 if(flag){
 		key_value = 1
 	 }
 	 var json = "{\"keyName\":\""+id+"\",\"keyValue\":\""+key_value+"\"}";
     $.ajax({
     	 type:"post",
         contentType:"application/json; charset=UTF-8",
         url: ctx + "/setting/update",
         data:json,
         dataType:"json",
         success: function(msg){
        	 if(id == "offlinePay"){
    	 		switchery.enable(); 
    	 	 }else if(id == "askForOfflinePay"){
    	 		switchery_t.enable();
    	 	 }else if(id == "orderRevisit"){
    	 		switchery_t.enable();
    	 	 }
        	 location.reload();
         },
         error: function (msg) {
        	$('#'+id+'').attr('disabled',false);
         	bootbox.alert("服务器偷了个小懒,修改基础设置失败了！");
         }
     });
}